`timescale 1ns/1ns

module tb_pid_cmd();

// 基本信号声明
reg clk;
reg rst_n;
reg pid_en;
reg device_0_en;
reg [31:0] target_count;
wire device_0_pwm;
wire stop_pluse;

// 新版物理过程模型
reg [31:0] particle_count;       // 直接累计计数器
localparam CLK_FREQ = 28'd500; //50_000_000;// 50MHz时钟频率

// 被测模块接口连接
pid_cmd u_pid(
    .clk(clk),
    .rst_n(rst_n),
    .pid_en(pid_en),
    .device_0_en(device_0_en),
    .count(particle_count),         // 直接连接计数器
    .target_count(target_count),
    .device_0_pwm(device_0_pwm),
    .stop_pluse(stop_pluse)
);

// 50MHz时钟生成
initial clk = 0;
always #10 clk = ~clk; // 20ns周期

// 秒级累计逻辑
always @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
        particle_count <= 0;
    end else begin      
        // 每次加占空比值
        particle_count <= particle_count + u_pid.duty_cycle;
    end
end

// 自动化测试场景
initial begin
    // 初始化记录
    $dumpfile("pid_wave.vcd");
    $dumpvars(0, tb_pid_cmd);
    
    // 变量初始化
    particle_count = 0;
    clk = 0;
    rst_n = 0;
    pid_en = 0;
    device_0_en = 0;
    target_count = 0;
    
    // 复位序列
    #100 rst_n = 1;
    @(posedge clk); // 同步等待
    #100 device_0_en = 1;
    
    // 测试场景：PID模式启动
    $display("[TEST] PID mode activation");
    target_count = 200000;
    pid_en = 0;
    
    // 设置仿真超时保护（20秒）
    #20_000_000; // 20_000_000_000ns = 20秒
    device_0_en  = 0;
    $display("Simulation timeout!");
    $finish;
end


// Duty cycle变化监控
reg [7:0] duty_cycle_prev = 0;
always @(posedge clk) begin
    if ($time > 10) begin // 避免初始不稳定状态
        if (u_pid.duty_cycle !== duty_cycle_prev) begin
            $display("T=%0tms: Duty cycle changed to %0d", 
                   $realtime/1000000.0,
                   u_pid.duty_cycle);
            duty_cycle_prev <= u_pid.duty_cycle;
        end
    end
end

endmodule
